/*
 * Map.cpp
 *
 *  Created on: 2010-05-27
 *      Author: mateusz
 */

#include "Map.h"

#include <stdlib.h>
#include <time.h>

//------------------------------------------------------------------------------

Map::Map(CL_GraphicContext &gc, CL_ResourceManager *resources) {
	wallImage = CL_Sprite(gc, "game/wall", resources);
	boxImage = CL_Sprite(gc, "game/box", resources);
	fireImage = CL_Sprite(gc, "game/fire", resources);
	bombImage = CL_Sprite(gc, "game/bomb", resources);

	size = 20;
	map = new int[(size+1)*(size+1)];

	for( int i = 0; i <= size*size; i++ ) {
		map[i] = 0;
	}
}

//------------------------------------------------------------------------------

Map::~Map() {
	if( map != NULL )
		delete[] map;
}

//------------------------------------------------------------------------------

void Map::draw(CL_GraphicContext &gc) {
	for( int i = 0; i < size; i++ ) {
		for( int j = 0; j < size; j++ ) {
			if( map[i*size+j] == 1 ) {
				wallImage.draw(gc, i*20, j*20);
			} else if( map[i*size+j] == 2 ) {
				boxImage.draw(gc, i*20, j*20);
			} else if( map[i*size+j] == 3 ) {
				bombImage.draw(gc, i*20, j*20);
			} else if( map[i*size+j] >= 4 ) {
				fireImage.draw(gc, i*20, j*20);
			}
		}
	}

	for( int i = 0; i < size*size; i++ ) {
		if( map[i] > 4 ) {
			map[i]--;
		} else if( map[i] == 4 ) {
			map[i] = 0;
		}
	}
}

//------------------------------------------------------------------------------

void Map::readMap(CL_String str) {
	for( unsigned i = 0; i < str.size(); i++ ) {
		if( str[i] == '2' )
			map[i] = 2;
		else if( str[i] == '1' )
			map[i] = 1;
		else if( str[i] == '0' )
			map[i] = 0;
	}
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
